<template>
  <div class="dialog-container">
    <el-dialog
      title="模板管理"
      :visible="dialogVisible"
      width="60%"
      :close-on-click-modal="false"
      @open="initData"
      @close="closeDialog"
    >
      <el-form
        ref="form"
        :model="dataForm"
        class="dialog-form"
        :rules="rules"
        label-width="110px"
        size="mini"
        :disabled="dialogType === 'detail'"
      >
        <!-- 自定义表格内容 -->
        <div class="custom-table-panel">
          <component :is="flag" />
        </div>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button :loading="saveLoading" class="sure-btn" size="mini" type="primary" @click="handleSave">保存</el-button>
        <el-button size="mini" class="cancel-btn" @click="closeDialog">取消</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import Form1 from './form/form1'
import Form2 from './form/form2'

export default {
  name: 'AddTemplateDialog',
  components: {
    Form1, Form2
  },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    dialogType: {
      type: String,
      default: 'detail'
    },
    obj: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      saveLoading: false,
      dataForm: {
        name: null
      },
      flag: null,
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }]
      }
    }
  },
  created() {

  },
  methods: {
    initData() {
      if (this.obj) {
        this.dataForm = JSON.parse(JSON.stringify(this.obj))
      }
      const flag = (new Date().getTime() % 2) === 0
      this.flag = flag ? 'Form1' : 'Form2'
    },
    handleSave() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          console.log(this.$refs.form1.getFormData())
        }
      })
    },
    closeDialog() {
      this.$emit('closeDialog')
      this.clearData()
      this.$refs['form'].clearValidate()
    },
    clearData() {
      this.dataForm = {
        name: null
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
